package com.yuan.pay_mall.service.impl;

import com.yuan.pay_mall.dao.CategoryMapper;
import com.yuan.pay_mall.pojo.Category;
import com.yuan.pay_mall.service.ICategoryService;
import com.yuan.pay_mall.vo.CategoryVo;
import com.yuan.pay_mall.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.yuan.pay_mall.consts.MallConst.ROOT_PARENT_ID;

/**
 * 耗时：http(请求微信api) >磁盘>内存
 * mysql(内网+磁盘)
 * 类目标
 */
@Service
@Slf4j
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public ResponseVo<List<CategoryVo>> selectAll() {
//        List<CategoryVo> categoryVoList = new ArrayList<>();
        List<Category> categories = categoryMapper.selectAll();//查询出来了数据库中所有的数据

        //查出parent_id=0，查一级根目录
       /* for (Category category:categories
             ) {
            if(category.getParentId().equals(ROOT_PARENT_ID)){
                CategoryVo categoryVo = new CategoryVo();
                //Spring自己的方法，拷贝，原来的，拷贝到哪
                BeanUtils.copyProperties(category,categoryVo);
                categoryVoList.add(categoryVo);
            }
        }*/
        //下面使用lambda表达式再重写   lambda + stream
        List<CategoryVo> categoryVoList = categories.stream()
                .filter(e -> e.getParentId().equals(ROOT_PARENT_ID))
                .map(this::category2CategoryVo)
                .sorted(Comparator.comparing(CategoryVo::getSortOrder).reversed())
                .collect(Collectors.toList());
        //查询子目录
        findSubCategory(categoryVoList,categories);




        return ResponseVo.success(categoryVoList);
    }

    @Override
    public void findSubCategoryId(Integer id, Set<Integer> resultSet) {
        List<Category> categories = categoryMapper.selectAll();
        findSubCategoryId(id,resultSet,categories);

    }

    public void findSubCategoryId(Integer id, Set<Integer> resultSet, List<Category> categories){
        for (Category category : categories) {
            if(category.getParentId().equals(id)){
                resultSet.add(category.getId());
                findSubCategoryId(category.getId(), resultSet,categories);
            }
        }
    }
    //查询子目录
    private void findSubCategory(List<CategoryVo> categoryVoList,List<Category> categories){
        for (CategoryVo categoryVo:categoryVoList //遍历整个数据库，查到一级目录
             ) {
            //写一个集合去装查出来的子目录
            List<CategoryVo> subCategoryVoList = new ArrayList<>();
            for (Category category : categories) { //查子目录
                //如果查到内容，设置subCategory,继续往下查
                if(categoryVo.getId().equals(category.getParentId())){
                    CategoryVo  subCategoryVo = category2CategoryVo(category);
                    subCategoryVoList.add(subCategoryVo);
                }
                //::方法引用http://t.csdn.cn/XcewS，lambda方法的变形
                subCategoryVoList.sort(Comparator.comparing(CategoryVo::getSortOrder).reversed());
                categoryVo.setSubCategories(subCategoryVoList);
                //递归，往下查
                findSubCategory(subCategoryVoList,categories);
            }
        }
    }



    private CategoryVo category2CategoryVo(Category category){
        //从大类中选出要返回发给页面显示的种类信息
        CategoryVo categoryVo = new CategoryVo();
        //Spring自己的方法，拷贝，原来的，拷贝到哪
        BeanUtils.copyProperties(category,categoryVo);
        return categoryVo;

    }
}


